﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace InputManager.Helpers
{
    [Serializable()]
    public class LimitedStack<T> : LinkedList<T>
    {
        private int _maxItems;

        public int MaxItems
        {
            get { return _maxItems; }
            set
            {
                while (this.Count > value)
                {
                    this.RemoveFirst();
                }
                _maxItems = value;
            }
        }

        public LimitedStack(int num)
        {
            _maxItems = num;
        }

        public T Peek()
        {
            return this.Last.Value;
        }

        public T Pop()
        {
            LinkedListNode<T> node = this.Last;
            this.RemoveLast();
            return node.Value;
        }

        public void Push(T value)
        {
            LinkedListNode<T> node = new LinkedListNode<T>(value);
            this.AddLast(node);

            if (this.Count > _maxItems)
            {
                this.RemoveFirst();
            }
        }
    }
}
